import 'package:BeeApp/MainPage/MainToolPage.dart';
import 'package:BeeApp/ShopPage/UserMainPage01.dart';
import 'package:BeeApp/ShopPage/UserMainPage02.dart';
import 'package:BeeApp/ShopPage/UserMainPage03.dart';
import 'package:BeeApp/ShopPage/UserManagePage.dart';
import 'package:BeeApp/ShopPage/UserNewsPage.dart';
import 'package:flutter/material.dart';

class MainUserPage extends StatefulWidget {
  MainUserPage({Key key}) : super(key: key);

  @override
  _MainUserPageState createState() => _MainUserPageState();
}

class _MainUserPageState extends State<MainUserPage> {
  TabController _controller;
  List pages = [UserMainPage01(), UserMainPage02(), UserMainPage03()];
  int selectPage = 0;

  @override
  void initState() {
    super.initState();
    _controller = TabController(
      length: pages.length,
      vsync: ScrollableState(),
    );
    _controller.addListener(
      () {
        if (_controller.index.toDouble() == _controller.animation.value) {
          //赋值 并更新数据
          this.setState(() {
            selectPage = _controller.index;
            print(_controller.index);
            if (_controller.index == 0) {
              print("全部订单");
            } else if (_controller.index == 1) {
              print("已处理订单");
            } else {
              print("未处理订单");
            }
          });
        }
      },
    );
  }

  @override
  void dispose() {
    super.dispose();
    _controller?.removeListener(() {});
    _controller?.dispose();
  }

  TextStyle textStyle01(int index) {
    if (index == selectPage) {
      return TextStyle(
        color: Color(0xFF333333),
        fontSize: setFontSize(36),
        fontWeight: FontWeight.bold,
      );
    } else {
      return TextStyle(
        color: Color(0xFFCBCBCB),
        fontSize: setFontSize(34),
      );
    }
  }

  Widget _getTabbar() {
    return Expanded(
      child: TabBar(
        controller: _controller,
        indicatorColor: Colors.black,
        labelPadding: EdgeInsets.fromLTRB(0, 0, 0, 2),
        indicatorWeight: 4,
        indicatorSize: TabBarIndicatorSize.label,
        indicatorPadding: EdgeInsets.only(left: 20, right: 20),
        // isScrollable: true,
        onTap: (value) {
          setState(() {
            selectPage = value;
          });
        },
        tabs: <Widget>[
          Text("同城物流", style: textStyle01(0)),
          Text("跑腿快送", style: textStyle01(1)),
          // RichText(
          //   text: TextSpan(children: <InlineSpan>[
          //     TextSpan(text: '国内', style: textStyle01(1)),
          //     TextSpan(
          //       text: '(同城)',
          //       style: TextStyle(
          //         color:
          //             selectPage == 1 ? Color(0xFF333333) : Color(0xFFCBCBCB),
          //         fontSize: setFontSize(20),
          //         fontWeight:
          //             selectPage == 1 ? FontWeight.bold : FontWeight.w500,
          //       ),
          //     ),
          //     TextSpan(text: '专运', style: textStyle01(1)),
          //   ]),
          // ),
          Text("我的订单", style: textStyle01(2)),
        ],
      ),
    );
  }

  Widget _getTabview() {
    return TabBarView(
      controller: _controller,
      children: <Widget>[
        UserMainPage01(),
        UserMainPage02(),
        UserMainPage03(),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text(
          "运小小",
          style: TextStyle(
            color: Color(0xFFFF821C),
            fontWeight: FontWeight.bold,
          ),
        ),
        elevation: 0,
        centerTitle: true,
        leading: IconButton(
          icon: Image.asset(
            "images/MainUserImage.png",
            height: 15,
            width: 15,
          ),
          onPressed: () {
            Navigator.push(context,
                MaterialPageRoute(builder: (context) => UserNewsPage()));
          },
        ),
        actions: [
          IconButton(
            icon: Image.asset(
              "images/NewsListImage.png",
              height: 20,
              width: 20,
              color: Colors.black,
            ),
            onPressed: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) => UserManagePage()));
            },
          )
        ],
      ),
      body: Container(
        // color: Colors.red,
        child: Column(
          children: <Widget>[
            Container(
              alignment: Alignment.topLeft,
              child: Row(
                children: <Widget>[
                  _getTabbar(),
                ],
              ),
            ),
            Expanded(child: _getTabview()),
          ],
        ),
      ),
    );
  }
}
